<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%-- <%@ taglib prefix="sf" uri="http://www.sf-express.com/tags" %> --%>

<!-- <sf:token id="user_role_bind"/>
<sf:token id="user_role_unbind"/> -->

<div class="modal-header r_location">
	<i class="r_txtico"></i>运营管理 &gt; 运营角色绑定
</div>

<form id="uForm" action="">
	<ul class="all_list">
		<li>
			<span class="all_title">用户名：</span>
			<span>
				<input type="text" id="username" name="userName" value="" maxlength="8" class="ui_input" validator='{"label":"用户名","required":false,"isDigit":true,"maxLength":8,"minLength":6}'/>
			</span>
		</li>
		<li>
			<span class="all_title">真实姓名：</span>
			<span>
				<input type="text" id="realName" name="realName" value="" class="ui_input" maxlength="25"/>
			</span>
		</li>
		<li>
			<span class="all_title">角色名：</span>
			<span>
				<input type="text" id="roleName" name="roleName" value="" class="ui_input" maxlength="25"/>
			</span>
		</li>
		<li>
			<span class="all_title">用户状态：</span>
			<span>
				<select id="userStatus" name="status" class="ui_input">
			  	<option value="">请选择</option>
			  	<option value="normal">正常</option>
			  	<option value="lock">停用</option>
			  </select>
			</span>
		</li>
		<li>
			<span class="all_title">所在机构：</span><span class="org_tree" id="org_tree"></span>
		</li>
		<li class="m-t10">
			<span class="all_title"></span>
			<span>
				<input type="button" class="search_btn m-r15 submitBtn" value="查 询" name="button" />
				<input type="reset" class="reset_btn m-r15 resetBtn" value="重 置" name="button" id="resetBtn">
				<!-- <input type="button" class="btn_blue" value="导出" id="exportBtn" /> -->
			</span>
		</li>
	</ul>
	<div class="clearfix table_fn">
		<span class="l">用户角色绑定列表</span>
  			<span class="r" style="_margin-top:5px;">
  			<shiro:hasPermission name="user_role:update">
  				<a href="javascript:void(0)" title="" class="add_ico" onclick="addDialog()" id="add_term">角色绑定</a>
  			</shiro:hasPermission>
  		</span>
	</div>
</form>

<div class="clearfix">
	<table id="gridTable">
	</table>
	<div id="pgtoolbar1"></div>
</div>


<script type="text/javascript">
$("#org_tree").searchTree({
	orgId:'orgId'
});
	var myDialog;
	$(function(){
		
		$("#exportBtn").click(function(){
			if((!$("#orgId") || $("#orgId").val() =="")){
				if(($("#userName").val()=="" || $("#userName").val()==null) && ($("#realName").val()=="" || $("#userName").val()==null)){
					art.dialog({icon: "error",content: "导出时请至少选择一个除用户状态外的条件",lock:true});
					return;
				}
				$('#uForm').attr("action","user_role/export");
				$('#uForm').get(0).submit();
			}else{
				jQuery.ajax({
					url: 'user_role/check_org?orgId='+$("#orgId").val()+'&rand='+Math.random(),
					success: function (data) {
						if(data.success == 'error'){
							art.dialog({icon: "error",content: data.message,lock:true});
							return;
						}else{
							$('#uForm').attr("action","user_role/export");
							$('#uForm').get(0).submit();
						}
					}
				});	
			}
			
		});
		$.jgrid.ajaxOptions.type = 'post';
	    $("#gridTable").jqGrid({
       		url:'user_role/view',
       		datatype: "json",
       		autowidth:true, 
       		rownumbers:true,
            height: 270,
            colModel:[
                 {		label:'用户ID',
      					name:'userId',
      					hidden:true
      			},
                {
					label:'用户名',
					name:'userName',
					width:70,
					sortable: false
				},
                {
					label:'真实姓名',
					name:'realName',
					width:70,
					sortable: false
				},
                {
					label:'联系电话',
					name:'telPhone',
					width:95, 
					sortable: false
				},
                {
					label:'邮箱',
					name:'email',
					width:120, 
					sortable: false
				},  
                {
					label:'用户状态',
					name:'status', 
					width:80,
					sortable: false,
					formatter : function (value, options, row) {
						return value=='normal' ? '正常' : '停用';
					}
				},
				{
					label:'机构代码',
					name:'orgCode',
					width:100,
					sortable: false
				},
                {
					label:'机构名称',
					name:'orgName',
					width:100,
					sortable: false
				},
                {
					label:'角色',
					name:'roleName',
					width : 120,
					align : 'left'
				},
                {
					label:'操作',
					name:'oper',
					width:120, 
					sortable: false,
					formatter : function (value, options, row) {
						var result = '';
						<shiro:hasPermission name="user_role:bind">
							result += '<a class="btn" href="javascript:void(0)" onclick="unbindRole('+row.userId+')">解绑</a>';
						</shiro:hasPermission>
						<shiro:hasPermission name="user_role:unbind">
						result += '&nbsp;&nbsp;<a class="btn" href="javascript:void(0)" onclick="setUserRoleDialog('+row.userId+')">修改角色</a>';
					</shiro:hasPermission>
						return result;
					}
				}
            ],
            viewrecords:true,
            rowNum:10,
            rowList:[10,20],
            jsonReader: {  
            	root:"data",
                page:"index",
                total:"total",
                records:"totalRecord",    
                repeatitems : false,
                id: 'cardId'
            },
            pager:"#pgtoolbar1"
		});
	});
	$("#uForm").validator({},{
		callback:function(results){
			search();
		}
	});
	function search(){
		var param = {};
		var paramArray = $('#uForm').serializeArray();
		for (var i in paramArray) {
			param[paramArray[i].name] = paramArray[i].value;
		}
		
		var postData = $("#gridTable").jqGrid('getGridParam', 'postData');
		$.extend(postData, param);
		$("#gridTable").jqGrid("setGridParam").trigger("reloadGrid", [{page:1}]);   
	}
	
	//新增用户绑定角色
	function addDialog(){
		art.dialog({
			lock: true,
			id:'addDialog',
			title:'人员查询',
			width:300,
			content:'用户名：<input type="text" id="userName" maxlength="8" class="ui_input"/>',
			button:[{name:'查询',
				callback:function(){
					if($.trim($("#userName").val()) == "" ){
						art.dialog({icon: "error",content: "用户名不能为空",lock:true});
						return false;
					}
					if(!/^([0-9]*)$/.test($.trim($("#userName").val()))){
						art.dialog({icon: "error",content: "用户名必须为数字",lock:true});
						return false;
					}
					check_user($("#userName").val());
					return false;
				},
				focus: true
			},{name:'重置',
				callback:function(){
					$("#userName").val("");
					return false;
				}
			}]
		});
	}
	
	/* 检查用户是否存在 */
	function check_user(username){
		jQuery.ajax({
			url: 'user_role/bind/check?userName='+username+'&rand='+Math.random(),
			success: function (data) {
				if(data.code == "00" || data.code == "02"){
					art.dialog({icon: "error",content: data.message,lock:true});
					return;
				}else if(data.code == "01"){
					setUserRoleDialog(data.message);
				}
			}
		});	
	}
	
	/* 准备绑定信息 */
	function setUserRoleDialog(userId){
		if(art.dialog.list["addDialog"]){
			art.dialog.list["addDialog"].close();
		}
		showDialog('user_role/bind/show_user_role?userId='+userId+'&rand='+Math.random(),
		{
			id:'setRoleDialog',
			width:'700px',
			title:'综合信息',
			lock:true,
			button:[{name:'关闭',callback:function(){
				if(art.dialog.list["setRoleDialog"]){
					art.dialog.list["setRoleDialog"].close();
				}
				}}, {name:'绑定',callback:function(){
					bind_permiss();
					return false;
				},focus: true}]
			}
		);
	}
	
	/* 提交绑定信息 */
	function bind_permiss(){
		var userId = $("#userId").val();
		var lis = $("#yes_limit li");
		var array = new Array();
		for(var i=0;i<lis.length;i++){
			array.push(lis[i].id);
		}
		jQuery.ajax({
			url: 'user_role/bind?userId='+userId+'&roleList='+array,
			tokenId:'user_role_bind',
			success: function (data) {
				if(data.success=='succeed'){
					art.dialog.list["setRoleDialog"].close();
				}
				 art.dialog({icon: data.success,content: data.message,lock:true});
				 $("#gridTable").trigger("reloadGrid", [{page:1}]);
			}
		});	
	}
	
	/* 解除绑定 */
	function unbindRole(userId){
		myDialog = art.dialog({
			lock:true,
		    content: '是否确定解绑?',
		    ok: function () {
		    	jQuery.ajax({
		    		tokenId: 'user_role_unbind',
					url: "user_role/unbind?userId="+userId+"&rand="+Math.random(),
					success: function (data) {
						art.dialog({icon: data.success,content: data.message,lock:true});
			        	$("#gridTable").trigger("reloadGrid", [{page:1}]);
					}
				});
		    },
		    cancelVal: '关闭',
		    cancel: true //为true等价于function(){}
		});
	}
	
	
	function showDialog(url,options){
		myDialog = art.dialog(options);// 初始化一个带有loading图标的空对话框
		jQuery.ajax({url: url,success: function (data) {
				myDialog.content(data);// 填充对话框内容
			}
		});
	}
</script>